package com.dizner.dimagepicker;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dizner.dimagepicker.loader.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class DImageListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Image> mDataList;

    private TreeMap<Integer, Boolean> selectionMap;
    private int mMax = 1;
    private OnPickFinishedCallback mPickFinishedCallback;

    private ImageLoader imageLoader;

    private boolean isSignale;

    DImageListAdapter(Context context) {
        this.mContext = context;
        mDataList = new ArrayList<>();
    }

    DImageListAdapter(Context context, ImageLoader imageLoader, OnPickFinishedCallback pickFinishedCallback) {
        this(context);
        this.imageLoader = imageLoader;
        this.mPickFinishedCallback = pickFinishedCallback;
    }

    public void setPickFinishedCallback(OnPickFinishedCallback pickFinishedCallback) {
        this.mPickFinishedCallback = pickFinishedCallback;
    }

    void setMax(int max) {
        this.mMax = max;
    }

    void setSignale(boolean signale) {
        isSignale = signale;
    }

    void setData(List<Image> dataList) {
        mDataList.clear();
        if (dataList != null) {
            mDataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Image getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    List<Image> getSelectionList() {
        List<Image> result = new ArrayList<>();
        if (selectionMap != null) {
            for (Integer key : selectionMap.keySet()) {
                if (key >= 0 && key < mDataList.size()) {
                    result.add(mDataList.get(key));
                }
            }
        }
        return result;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_image_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Image image = mDataList.get(position);
        imageLoader.onLoadImage(mContext, image.getUri(), holder.mImageView);

        if (selectionMap == null) {
            selectionMap = new TreeMap<>();
        }
        if (isSignale) {
            holder.mCb.setVisibility(View.GONE);
            holder.mImageViewShadow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectionMap.put(position, true);
                    if (mPickFinishedCallback != null) {
                        mPickFinishedCallback.onPick(getSelectionList());
                        mPickFinishedCallback.onPickFinished(getSelectionList());
                    }
                }
            });
        } else {
            holder.mCb.setVisibility(View.VISIBLE);
            holder.mCb.setOnCheckedChangeListener(null);
            holder.mCb.setChecked(selectionMap.containsKey(position));
            holder.mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (selectionMap.size() >= mMax) {
                            holder.mCb.setChecked(false);
                            showToast(String.format("最多可选%s张", mMax));
                        } else {
                            selectionMap.put(position, true);
                            if (selectionMap.size() >= mMax) {
                                if (mPickFinishedCallback != null) {
                                    mPickFinishedCallback.onPickFinished(getSelectionList());
                                }
                            }
                        }
                    } else {
                        selectionMap.remove(position);
                    }
                    if (mPickFinishedCallback != null) {
                        mPickFinishedCallback.onPick(getSelectionList());
                    }
                }
            });
        }

        return convertView;
    }

    private void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    static class ViewHolder {
        private ImageView mImageView;
        private View mImageViewShadow;
        private CheckBox mCb;

        ViewHolder(View rootView) {
            mImageView = rootView.findViewById(R.id.iv_image_content);
            mImageViewShadow = rootView.findViewById(R.id.view_image_content_shadow);
            mCb = rootView.findViewById(R.id.cb_item_select);
        }
    }

    interface OnPickFinishedCallback {
        void onPickFinished(List<Image> result);

        void onPick(List<Image> result);
    }

}
